package com.lingshu.app.ui.report;

import androidx.lifecycle.ViewModelProvider;

import com.lingshu.app.R;
import com.lingshu.app.base.BaseFragment;
import com.lingshu.app.config.Constants;
import com.lingshu.app.databinding.FragmentFormBreastBinding;
import com.lingshu.app.model.register.RegisterResponse;
import com.lingshu.app.model.register.RegisterScreenshotResponse;
import com.lingshu.core.eventbus.EventBusManager;
import com.lingshu.core.eventbus.EventData;
import com.lingshu.core.eventbus.EventKey;
import com.lingshu.customview.CounterView;

import java.math.BigDecimal;

/**
 * 乳腺智能报告 Fragment
 */
public class FormBreastFragment extends BaseFragment<FragmentFormBreastBinding, FormBreastFragmentViewModel> {
    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_form_breast;
    }

    @Override
    protected void initViewModel() {
        viewModel = new ViewModelProvider(this).get(FormBreastFragmentViewModel.class);
    }

    @Override
    protected void bindViewModel() {
        dataBinding.setViewModel(viewModel);
    }

    @Override
    protected void init() {
        if (getArguments() != null) {
            viewModel.registerInfo = (RegisterResponse) getArguments().getSerializable(Constants.REGISTER_INFO);
            viewModel.screenshotResponse1 = (RegisterScreenshotResponse) getArguments().getSerializable(Constants.SCREENSHOT1);
            viewModel.screenshotResponse2 = (RegisterScreenshotResponse) getArguments().getSerializable(Constants.SCREENSHOT2);
            viewModel.loadData();
            viewModel.allSingleReport.postValue(viewModel.registerInfo.getAllowSingleSide());
            viewModel.rightIsConfirm.postValue(viewModel.registerInfo.getRightIsConfirmed());
            viewModel.leftIsConfirm.postValue(viewModel.registerInfo.getLeftIsConfirmed());
        }
        viewModel.initData();
        observeData();
        counterListener();
    }

    /**
     * counterView初始化
     *
     * @param counterView
     * @param defaultValue
     * @param minValue
     * @param maxValue
     */
    private void InitCounterView(CounterView counterView, long defaultValue, long minValue, long maxValue) {
        counterView.setMinValue(BigDecimal.valueOf(minValue));
        counterView.setMaxValue(BigDecimal.valueOf(maxValue));
        counterView.setIncrement(BigDecimal.valueOf(1));
        counterView.setDefaultValue(BigDecimal.valueOf(defaultValue));
    }

    /**
     * counter事件监听
     */
    private void counterListener() {
        InitCounterView(dataBinding.counterBreastWidthRight, 0, 0, 0);
        InitCounterView(dataBinding.counterBreastHeightRight, 0, 0, 0);
        InitCounterView(dataBinding.counterBreastWidthLeft, 0, 0, 0);
        InitCounterView(dataBinding.counterBreastHeightLeft, 0, 0, 0);
        dataBinding.counterBreastWidthRight.setOnValueChangedListener(newValue -> viewModel.structureData.getValue().setWidthBreastR(newValue.doubleValue()));
        dataBinding.counterBreastHeightRight.setOnValueChangedListener(newValue -> viewModel.structureData.getValue().setHeightBreastR(newValue.doubleValue()));
        dataBinding.counterBreastWidthLeft.setOnValueChangedListener(newValue -> viewModel.structureData.getValue().setWidthBreastL(newValue.doubleValue()));
        dataBinding.counterBreastHeightLeft.setOnValueChangedListener(newValue -> viewModel.structureData.getValue().setHeightBreastL(newValue.doubleValue()));
    }

    /**
     * 监控数据变化
     */
    private void observeData() {
        //结构化数据
        viewModel.structureData.observe(this, data -> {
            if (data.getExceptionalBreastR() >= 2) {
                InitCounterView(dataBinding.counterBreastWidthRight, data.getWidthBreastR().longValue(), 1, 20);
                InitCounterView(dataBinding.counterBreastHeightRight, data.getHeightBreastR().longValue(), 1, 20);
            } else {
                InitCounterView(dataBinding.counterBreastWidthRight, 0, 0, 0);
                InitCounterView(dataBinding.counterBreastHeightRight, 0, 0, 0);
            }

            if (data.getExceptionalBreastL() >= 2) {
                InitCounterView(dataBinding.counterBreastWidthLeft, data.getWidthBreastL().longValue(), 1, 20);
                InitCounterView(dataBinding.counterBreastHeightLeft, data.getHeightBreastL().longValue(), 1, 20);
            } else {
                InitCounterView(dataBinding.counterBreastWidthLeft, 0, 0, 0);
                InitCounterView(dataBinding.counterBreastHeightLeft, 0, 0, 0);
            }
        });

        //保存报告 触发
        viewModel.reportSaveResponse.observe(this, data -> {
            viewModel.registerInfo = data.getRegisterInfo();
            viewModel.setStructureData(data.getBreastForm());
            viewModel.structureData.postValue(data.getBreastForm());
            viewModel.canSave.postValue(true);
            viewModel.canAudit.postValue(true);
        });

        //审核报告 触发
        viewModel.reportAuditResponse.observe(this, data -> {
            viewModel.registerInfo = data;
            viewModel.canSave.postValue(false);
            viewModel.canAudit.postValue(false);

            getActivity().finish();
            EventBusManager.post(new EventData<>(EventKey.Refresh, "refresh"));
        });

        //右侧变化 触发
        viewModel.allowInputR.observe(this, data -> {
            if (!data) {
                InitCounterView(dataBinding.counterBreastWidthRight, 0, 0, 0);
                InitCounterView(dataBinding.counterBreastHeightRight, 0, 0, 0);
            } else {
                InitCounterView(dataBinding.counterBreastWidthRight, 1, 1, 20);
                InitCounterView(dataBinding.counterBreastHeightRight, 1, 1, 20);
            }
        });

        //左侧变化 触发
        viewModel.allowInputL.observe(this, data -> {
            if (!data) {
                InitCounterView(dataBinding.counterBreastWidthLeft, 0, 0, 0);
                InitCounterView(dataBinding.counterBreastHeightLeft, 0, 0, 0);
            } else {
                InitCounterView(dataBinding.counterBreastWidthLeft, 1, 1, 20);
                InitCounterView(dataBinding.counterBreastHeightLeft, 1, 1, 20);
            }
        });
    }
}
